const Bpmn = require('bpmn-engine');
const EventEmitter = require('events').EventEmitter;
const fs = require('fs');

let engine;
const activityIds = [];
const lineIds = [];
const curNodeIds = [];
let callbackFun;
let dataCs;
let errCs;


function setListener() {
    const listener = new EventEmitter();
    listener.on('taken', (flow) => {
        lineIds.push(flow.id);
    });
    for (var i = 0; i < dataCs['nodeIds'].length; i++) {
        listener.once('wait-' + dataCs['nodeIds'][i], (child, instance) => {
            activityIds.push(child.activity.id);
            instance.signal(child.activity.id, {
                sirname: 's'
            });
        });
    }

    return listener;
}
function eeeee(data, callback) {
    callbackFun = callback;
    dataCs = data;

    // 基本执行
    engine.execute({
        variables: data.variables,
        listener: setListener()
    },(err, definition) => {
        if (err) {
            console.log(err);
        };
        console.log('=================');
    });
};

engine = new Bpmn.Engine({
    source: fs.readFileSync('./ass/1004.bpmn'),
    moddleOptions: {
        camunda: require('camunda-bpmn-moddle/resources/camunda')
    }
});

setTimeout(() => {
    Bpmn.Engine.resume(engine.getState(), {
        listener: setListener()
    },  (err, definition) => {
        console.log('333333333');
    });

    setTimeout(() => {
        console.log('------------------------------------');
        const activitys = engine.getPendingActivities().definitions[0].children;
        console.log(JSON.stringify(engine.getPendingActivities().definitions));
        console.log('-----------------------------------');
        for (var i = 0; i < activitys.length; i++) {
            curNodeIds.push(activitys[i].id);
            if (activitys.children != undefined) {
                const childrens = activitys.children;

            }
        }
        const dddd = { activityIds: activityIds, curNodeIds: curNodeIds, state: '', lineIds: lineIds };
        console.log(JSON.stringify(dddd));
    }, 600);
}, 300);

eeeee({
    state: "",
    engineName: '',
    nodeIds: ['UserTask_1fis4zu', 'UserTask_09l4c4c', 'UserTask_0ytm91g', 'UserTask_0agqui6', 'UserTask_1p6t0tx',
        "UserTask_0dryav8", "UserTask_0o1k9vf", "UserTask_1vqa3hz", "UserTask_0s5t61g", "UserTask_1vzdkr2"],
    variables: {level: "90", cashAmount: "1000"}
}, null);

